﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.Utility;

using System.IO;
using Aliyeye.Car.Net.UI.AccountsUsersModel;
using Aliyeye.Car.Net.UI.Components;

namespace Aliyeye.Car.Net.UI.AdminCustomer
{
    public partial class CustomerPer : BaseForm
    {
        CustomerSvc.SoapHeaderClass sopaHeaderCustomer = new CustomerSvc.SoapHeaderClass();
        private bool isDriver = false;
        private int CustId = 0;
        CustomerSvc.Customer model = new CustomerSvc.Customer();

        public CustomerPer()
        {
            InitializeComponent();
        }
        public CustomerPer(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

        public CustomerPer(bool isDri, int id)
        {
            InitializeComponent();
            isDriver = isDri;
            CustId = id;
            this.Text = "修改客户信息";
        }

        public CustomerPer(bool isDri, int id, bool isView)
        {
            InitializeComponent();
            isDriver = isDri;
            CustId = id;
            if (isView)
            {
                this.Text = "查看客户信息";

                foreach (Control con in tabPage1.Controls)
                    con.Enabled = false;

                foreach (Control con in tabPage2.Controls)
                    con.Enabled = false;

                foreach (Control con in tabPage3.Controls)
                    con.Enabled = false;

                foreach (Control con in tabPage4.Controls)
                    con.Enabled = false;

                foreach (Control con in tabPage5.Controls)
                    con.Enabled = false;

                btnAdd.Text = "修改";

                panelAccount.Enabled = true;
                panelDriveLicense.Enabled = true;
                panelID.Enabled = true;
                panelCreditCard.Enabled = true;
                panelUser.Enabled = true;
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text.Trim() == "修改")
            {
                this.Text = "修改客户信息";

                foreach (Control con in tabPage1.Controls)
                    con.Enabled = true;

                foreach (Control con in tabPage2.Controls)
                    con.Enabled = true;

                foreach (Control con in tabPage3.Controls)
                    con.Enabled = true;

                foreach (Control con in tabPage4.Controls)
                    con.Enabled = true;

                foreach (Control con in tabPage5.Controls)
                    con.Enabled = true;

                btnAdd.Text = "提交";

                return;
            }

            if (string.IsNullOrEmpty(txtname.Text))
            {
                DialogWarning dia = new DialogWarning("客户姓名不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(txtaddr.Text))
            {
                DialogWarning dia = new DialogWarning("家庭地址不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(txtCodeCar.Text))
            {
                DialogWarning dia = new DialogWarning("身份证号码不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(txtComName.Text))
            {
                DialogWarning dia = new DialogWarning("单位名称不能为空！");
                dia.ShowDialog();
                return;
            }

            //if (string.IsNullOrEmpty(txtComAddr.Text))
            //{
            //    DialogWarning dia = new DialogWarning("单位地址不能为空！");
            //    dia.ShowDialog();
            //    return;
            //}

            if (string.IsNullOrEmpty(txttel.Text))
            {
                DialogWarning dia = new DialogWarning("移动电话不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(txtPhone.Text))
            {
                DialogWarning dia = new DialogWarning("联系电话不能为空！");
                dia.ShowDialog();
                return;
            }

            //if (loginUserID == 0)
            //{
            //    PageCommon.Alert("请选择登陆账号！");
            //    return;
            //}

            overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            //BLL.Customer bll = new BLL.Customer();
            CustomerSvc.Customer model = new CustomerSvc.Customer();

            model.Name = txtname.Text;
            model.Marks = txtMarks.Text;
            model.Tel = txttel.Text;
            model.Phone = txtPhone.Text;
            model.Code = txtCode.Text;
            model.Cost = decimal.Parse(txtcost.Text);
            model.Sex = Cbsex.Text;
            model.Addr = txtaddr.Text;

            model.ComName = txtComName.Text;
            model.CodeCar = txtCodeCar.Text;
            model.BondsMan = txtBondsMan.Text;
            model.BondTel = txtBondTel.Text;
            model.BondSex = cmbBondSex.Text;
            model.BondComName = txtBondComName.Text;
            model.BondAddr = txtBondAddr.Text;
            model.BondCar = txtBondCar.Text;
            model.Relation = txtRelation.Text;

            model.PaidIn = decimal.Parse(txtPaidIn.Text);
            model.Owing = decimal.Parse(txtOwing.Text);
            model.Path = "";
            model.Custype = 1;
            model.InputUserId = Aliyeye.Car.Net.UI.SystemHelp.Login.LogUserId;

            model.Driver = txtDriver.Text;
            model.DriSex = cbmDriSex.Text;
            model.DriCode = txtDriCode.Text;
            model.DriTel = txtDriTel.Text;
            model.DriAddr = txtDriAddr.Text;
            model.DriCard = txtDriCard.Text;
            model.JionCard = txtJionCard.Text;
            model.CarType = cmbCarType.Text;
            model.CareTime = txtCareTime.Value;
            model.OnDri = txtOnDri.Value;
            model.GetCar = txtGetCar.Value;
            model.ComAddr = txtComAddr.Text;
            model.CusCartype = CmbCusCartype.Text;
            model.CustCar = txtCustCar.Value;
            model.InpuTime = DateTime.Now;
            model.CardTime = txtCardTime.Value;
            model.IsState = true;

            model.Company = "";
            model.CompAddr = "";
            model.LegalPer = "";
            model.ComCode = "";
            model.Capital = "";
            model.OrgCode = "";

            model.pictureBusinessLicense = new byte[0];
            model.pictureTax = new byte[0];

            if (mStreamUser != null && isUpdateForUser)
                model.pictureUser = mStreamUser.GetBuffer();  //添加，更新时改了图片
            else if (mStreamUser == null && !isUpdateForUser)
                model.pictureUser = byteUser;   // 更新时没改图片
            else
                model.pictureUser = new byte[0];

            if (mStreamID != null && isUpdateForID)
                model.pictureID = mStreamUser.GetBuffer();
            else if (mStreamID == null && !isUpdateForID)
                model.pictureID = byteID;
            else
                model.pictureID = new byte[0];

            if (mStreamDriveLicense != null && isUpdateForDriveLicense)
                model.pictureDriveLicense = mStreamDriveLicense.GetBuffer();
            else if (mStreamDriveLicense == null && !isUpdateForDriveLicense)
                model.pictureDriveLicense = byteDriveLicense;
            else
                model.pictureDriveLicense = new byte[0];

            if (mStreamAccount != null && isUpdateForAccount)
                model.pictureAccount = mStreamAccount.GetBuffer();
            else if (mStreamAccount == null && !isUpdateForAccount)
                model.pictureAccount = byteAccount;
            else
                model.pictureAccount = new byte[0];

            if (mStreamCreditCard != null && isUpdateForCreditCard)
                model.pictureCreditCard = mStreamCreditCard.GetBuffer();
            else if (mStreamCreditCard == null && !isUpdateForCreditCard)
                model.pictureCreditCard = byteCreditCard;
            else
                model.pictureCreditCard = new byte[0];

            if (loginUserID != 0)
                model.UserID = loginUserID;

            if (CustId > 0)
            {
                model.Id = CustId;
                //bll.Update(model);
                //DialogNotify diaSecc = new DialogNotify("客户资料修改成功！");
                //diaSecc.ShowDialog();
                //this.Close();

                ServiceManipulator.SafeAsyncExecution<CustomerSvc.CustomerServiceSoapClient>(
                    proxy => proxy.Update(sopaHeaderCustomer, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("客户资料修改成功！");
                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                            this.Close();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
            else
            {
                //bll.Add(model);
                //DialogNotify diaSecc = new DialogNotify("客户资料添加成功！");
                //diaSecc.ShowDialog();
                //clear();
                ServiceManipulator.SafeAsyncExecution<CustomerSvc.CustomerServiceSoapClient>(
                    proxy => proxy.Add(sopaHeaderCustomer, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("客户资料添加成功！");
                            clear();
                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }


        }

        private void DataBin()
        {
            if (CustId > 0)
            {
                //BLL.Customer bll = new BLL.Customer();
                //Model.Customer model = bll.GetModel(CustId);                

                overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;
                Cursor.Current = Cursors.WaitCursor;
                ServiceManipulator.SafeAsyncExecution<CustomerSvc.CustomerServiceSoapClient>(
                    proxy => proxy.GetModel(sopaHeaderCustomer, CustId),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            model = (CustomerSvc.Customer)result.Result;
                            txtaddr.Text = model.Addr.ToString();
                            txtBondAddr.Text = model.BondAddr.ToString();
                            txtBondCar.Text = model.BondCar.ToString();
                            txtBondComName.Text = model.BondComName.ToString();

                            txtBondsMan.Text = model.BondsMan.ToString();
                            txtBondTel.Text = model.BondTel.ToString();
                            txtCardTime.Value = model.CardTime;
                            txtCareTime.Value = model.CareTime;
                            txtCode.Text = (model.Code).ToString();
                            txtCodeCar.Text = model.CodeCar.ToString();
                            txtComName.Text = model.ComName.ToString();
                            txtComAddr.Text = model.ComAddr.ToString();
                            txtcost.Text = model.Cost.ToString();

                            txtCustCar.Value = model.CustCar;
                            txtDriAddr.Text = model.DriAddr.ToString();
                            txtDriCard.Text = model.DriCard.ToString();
                            txtDriCode.Text = model.DriCode.ToString();
                            txtDriTel.Text = model.DriTel.ToString();
                            txtDriver.Text = model.Driver.ToString();
                            txtGetCar.Value = model.GetCar;
                            txtJionCard.Text = model.JionCard.ToString();
                            txtMarks.Text = model.Marks.ToString();

                            txtname.Text = model.Name.ToString();
                            txtOnDri.Value = model.OnDri;
                            txtOwing.Text = model.Owing.ToString();

                            txtPaidIn.Text = model.PaidIn.ToString();
                            txtPhone.Text = model.Phone.ToString();
                            txtRelation.Text = model.Relation.ToString();
                            txttel.Text = model.Tel.ToString();
                            Cbsex.Text = model.Sex.ToString();
                            cmbBondSex.Text = model.BondSex.ToString();
                            cbmDriSex.Text = model.DriSex.ToString();
                            cmbCarType.Text = model.CarType.ToString();
                            CmbCusCartype.Text = model.CusCartype.ToString();
                            if (model.pictureUser != null)
                            {
                                this.pictureUser.Image = byteToBitmap(model.pictureUser);
                                byteUser = model.pictureUser;
                            }
                            else
                            {
                                byteUser = new byte[0];
                            }
                            if (model.pictureID != null)
                            {
                                this.pictureID.Image = byteToBitmap(model.pictureID);
                                byteID = model.pictureID;
                            }
                            else
                            {
                                byteID = new byte[0];
                            }
                            if (model.pictureDriveLicense != null)
                            {
                                this.pictureDriveLicense.Image = byteToBitmap(model.pictureDriveLicense);
                                byteDriveLicense = model.pictureDriveLicense;
                            }
                            else
                            {
                                byteDriveLicense = new byte[0];
                            }
                            if (model.pictureAccount != null)
                            {
                                this.pictureAccount.Image = byteToBitmap(model.pictureAccount);
                                byteAccount = model.pictureAccount;
                            }
                            else
                            {
                                byteAccount = new byte[0];
                            }
                            if (model.pictureCreditCard != null)
                            {
                                this.pictureCreditCard.Image = byteToBitmap(model.pictureCreditCard);
                                byteCreditCard = model.pictureCreditCard;
                            }
                            else
                            {
                                byteCreditCard = new byte[0];
                            }
                            loginUserID = model.UserID;
                            loginUserAccount = model.LoginUserName;
                            txtUserAccount.Text = loginUserAccount;

                            lblPoints.Text = model.Points.ToString();

                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
        }

        public Bitmap byteToBitmap(byte[] imageBytes)
        {
            if (imageBytes.Length == 0 || imageBytes.Length < 0)
            {
                return null;
            }

            string imageType = Encoding.ASCII.GetString(imageBytes, 20, 12);
            MemoryStream stream;
            if (imageType == "Bitmap Image")
            {
                //如果数据库中保存的是BMP图形则需要去掉OLE头
                stream = new MemoryStream(imageBytes, 78, imageBytes.Length - 78);
            }
            else
            {
                stream = new MemoryStream(imageBytes, 0, imageBytes.Length);
            }

            Bitmap bmp = new Bitmap(stream);

            return bmp;
        }

        private void clear()
        {
            txtaddr.Text = "";
            txtBondAddr.Text = "";
            txtBondCar.Text = "";
            txtBondComName.Text = "";
            txtBondsMan.Text = "";
            txtBondTel.Text = "";
            txtCareTime.Value = DateTime.Now;
            txtCardTime.Value = DateTime.Now;
            txtCode.Text = "";
            txtCodeCar.Text = "";
            txtComName.Text = "";
            txtcost.Text = "0";
            txtCustCar.Value = DateTime.Now;
            txtDriAddr.Text = "";
            txtDriCard.Text = "";
            txtDriCode.Text = "";
            txtDriTel.Text = "";
            txtDriver.Text = "";
            txtGetCar.Value = DateTime.Now;
            txtJionCard.Text = "";
            txtMarks.Text = "";
            txtname.Text = "";
            txtOnDri.Value = DateTime.Now;
            txtOwing.Text = "0";
            txtPaidIn.Text = "0";
            txtPhone.Text = "";
            txtRelation.Text = "";
            txttel.Text = "";
            pictureUser.Image = null;
            pictureID.Image = null;
            pictureAccount.Image = null;
            pictureDriveLicense.Image = null;
            pictureCreditCard.Image = null;
            byteUser = new byte[0];
            byteID = new byte[0];
            byteDriveLicense = new byte[0];
            byteAccount = new byte[0];
            byteCreditCard = new byte[0];


        }
        private void menuBlueButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CustomerPer_Load(object sender, EventArgs e)
        {
            BindComboBoxInfo();
            DataBin();
        }

        private void BindComboBoxInfo()
        {
            overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            Cbsex.SelectedIndex = 0;
            cbmDriSex.SelectedIndex = 0;
            cmbBondSex.SelectedIndex = 0;
            ArchivesSvc.SoapHeaderClass sopaHeader = new ArchivesSvc.SoapHeaderClass();

            DataSet ds = null;

            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxCarX(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        CmbCusCartype.DisplayMember = "Namevalue";
                        CmbCusCartype.ValueMember = "Id";
                        CmbCusCartype.DataSource = ds.Tables[0];
                        CmbCusCartype.SelectedIndex = 0;

                        cmbCarType.DisplayMember = "Namevalue";
                        cmbCarType.ValueMember = "Id";
                        cmbCarType.DataSource = ds.Tables[0];
                        cmbCarType.SelectedIndex = 0;

                        overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private string picPathUser = "";
        private string picPathID = "";
        private string picPathDriveLicense = "";
        private string picPathAccount = "";
        private string picPathCreditCard = "";
        MemoryStream mStreamUser = null;
        MemoryStream mStreamID = null;
        MemoryStream mStreamDriveLicense = null;
        MemoryStream mStreamAccount = null;
        MemoryStream mStreamCreditCard = null;
        byte[] byteUser = new byte[0];
        byte[] byteID = new byte[0];
        byte[] byteDriveLicense = new byte[0];
        byte[] byteAccount = new byte[0];
        byte[] byteCreditCard = new byte[0];
        bool isUpdateForID = false;
        bool isUpdateForDriveLicense = false;
        bool isUpdateForAccount = false;
        bool isUpdateForCreditCard = false;
        bool isUpdateForUser = false;


        private void pictureUser_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogpictureUser.ShowDialog() == DialogResult.OK)
            {
                if (this.openFileDialogpictureUser.FileName.Trim() != "")
                {
                    picPathUser = this.openFileDialogpictureUser.FileName;
                }
                FileStream imageStream = new FileStream(picPathUser, FileMode.Open);
                mStreamUser = new MemoryStream();
                byte[] imageData = new byte[1024];
                int count = 0;
                while (0 < (count = imageStream.Read(imageData, 0, imageData.Length)))
                {
                    mStreamUser.Write(imageData, 0, count);
                }
                isUpdateForUser = true;
                pictureUser.Image = Image.FromStream(imageStream, false);
            }
        }

        private void pictureID_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogpictureID.ShowDialog() == DialogResult.OK)
            {
                if (this.openFileDialogpictureID.FileName.Trim() != "")
                {
                    picPathID = this.openFileDialogpictureID.FileName;
                }
                FileStream imageStream = new FileStream(picPathID, FileMode.Open);
                mStreamID = new MemoryStream();
                byte[] imageData = new byte[1024];
                int count = 0;
                while (0 < (count = imageStream.Read(imageData, 0, imageData.Length)))
                {
                    mStreamID.Write(imageData, 0, count);
                }
                isUpdateForID = true;
                pictureID.Image = Image.FromStream(imageStream, false);
            }

        }

        private void pictureDriveLicense_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogpictureDriveLicense.ShowDialog() == DialogResult.OK)
            {
                if (this.openFileDialogpictureDriveLicense.FileName.Trim() != "")
                {
                    picPathDriveLicense = this.openFileDialogpictureDriveLicense.FileName;
                }
                FileStream imageStream = new FileStream(picPathDriveLicense, FileMode.Open);
                mStreamDriveLicense = new MemoryStream();
                byte[] imageData = new byte[1024];
                int count = 0;
                while (0 < (count = imageStream.Read(imageData, 0, imageData.Length)))
                {
                    mStreamDriveLicense.Write(imageData, 0, count);
                }
                isUpdateForDriveLicense = true;
                pictureDriveLicense.Image = Image.FromStream(imageStream, false);
            }

        }

        private void pictureAccount_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogpictureAccount.ShowDialog() == DialogResult.OK)
            {
                if (this.openFileDialogpictureAccount.FileName.Trim() != "")
                {
                    picPathAccount = this.openFileDialogpictureAccount.FileName;
                }
                FileStream imageStream = new FileStream(picPathAccount, FileMode.Open);
                mStreamAccount = new MemoryStream();
                byte[] imageData = new byte[1024];
                int count = 0;
                while (0 < (count = imageStream.Read(imageData, 0, imageData.Length)))
                {
                    mStreamAccount.Write(imageData, 0, count);
                }
                isUpdateForAccount = true;
                pictureAccount.Image = Image.FromStream(imageStream, false);
            }

        }

        private void pictureCreditCard_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogpictureCreditCard.ShowDialog() == DialogResult.OK)
            {
                if (this.openFileDialogpictureCreditCard.FileName.Trim() != "")
                {
                    picPathCreditCard = this.openFileDialogpictureCreditCard.FileName;
                }
                FileStream imageStream = new FileStream(picPathCreditCard, FileMode.Open);
                mStreamCreditCard = new MemoryStream();
                byte[] imageData = new byte[1024];
                int count = 0;
                while (0 < (count = imageStream.Read(imageData, 0, imageData.Length)))
                {
                    mStreamCreditCard.Write(imageData, 0, count);
                }
                isUpdateForCreditCard = true;
                pictureCreditCard.Image = Image.FromStream(imageStream, false);
            }

        }

        private string loginUserAccount = "";
        private int loginUserID = 0;

        private void txtUserAccount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtUserAccount.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("登陆账号不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch);
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }

            string strSql = "select Accounts_Users.UserID,Accounts_Users.UserName,Accounts_Users.TrueName,Accounts_Users.Sex,Accounts_Users.State,Accounts_Users.Tel,Accounts_Roles.Description, Base_Department.ClassName from Accounts_Users left join Accounts_Roles on Accounts_Roles.RoleID=Accounts_Users.RoleId left join Base_Department on Base_Department.ClassID=Accounts_Users.DepID where Accounts_Roles.Description='客户' and( UserName like '%" + strSearch + "%' or TrueName like '%" + strSearch + "%' )";

            overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            DataSet ds = null;
            dgvInfo.DataSource = null;
            dgvInfo.AutoGenerateColumns = false;

            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetAllListForSearchCondistion(sopaHeader, strSql),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvInfo.DataSource = ds.Tables[0];
                            this.dgvInfo.Visible = true;
                            this.dgvInfo.Focus();
                        }
                        else
                        {
                            loginUserAccount = "";
                            txtUserAccount.Text = loginUserAccount;
                            loginUserID = 0;
                            PageCommon.Alert("查找的登陆账号不存在，请重新查找！");
                        }
                        overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                loginUserID = Convert.ToInt32(this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString());
                loginUserAccount = this.dgvInfo.CurrentRow.Cells["登录名"].Value.ToString();
                txtUserAccount.Text = loginUserAccount;
                this.dgvInfo.Visible = false;
            }
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            AddUsers form = new AddUsers(false);
            form.ShowDialog();
        }

        //下载个人照片
        private void panelUser_Click(object sender, EventArgs e)
        {
            if (pictureUser.Image == null)
            {
                PageCommon.Alert("请先上传后，再下载！");
                return;
            }
            //开始下载图片
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                Bitmap bitmapUser = (Bitmap)pictureUser.Image;
                bitmapUser.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                PageCommon.Notify("下载成功！");
            }
        }

        //下载身份证
        private void panelID_Click(object sender, EventArgs e)
        {
            if (pictureID.Image == null)
            {
                PageCommon.Alert("请先上传后，再下载！");
                return;
            }
            //开始下载图片
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                Bitmap bitmapID = (Bitmap)pictureID.Image;
                bitmapID.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                PageCommon.Notify("下载成功！");
            }
        }

        //下载驾驶证
        private void panelDriveLicense_Click(object sender, EventArgs e)
        {
            if (pictureDriveLicense.Image == null)
            {
                PageCommon.Alert("请先上传后，再下载！");
                return;
            }
            //开始下载图片
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                Bitmap bitmapDriveLicense = (Bitmap)pictureDriveLicense.Image;
                bitmapDriveLicense.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                PageCommon.Notify("下载成功！");
            }
        }

        //下载户口本
        private void panelAccount_Click(object sender, EventArgs e)
        {
            if (pictureAccount.Image == null)
            {
                PageCommon.Alert("请先上传后，再下载！");
                return;
            }
            //开始下载图片
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                Bitmap bitmapAccount = (Bitmap)pictureAccount.Image;
                bitmapAccount.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                PageCommon.Notify("下载成功！");
            }
        }

        //下载信用卡
        private void panelCreditCard_Click(object sender, EventArgs e)
        {
            if (pictureCreditCard.Image == null)
            {
                PageCommon.Alert("请先上传后，再下载！");
                return;
            }
            //开始下载图片
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                Bitmap bitmapCreditCard = (Bitmap)pictureCreditCard.Image;
                bitmapCreditCard.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                PageCommon.Notify("下载成功！");
            }
        }

        private void dgvPointsCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvPoints.Columns[e.ColumnIndex].Name == "订车类型")
            {
                if (e.Value != null)
                {
                    if (Convert.ToInt32(e.Value) == 1)
                    {
                        e.Value = "长租自驾";
                    }

                    else if (Convert.ToInt32(e.Value) == 2)
                    {
                        e.Value = "短租自驾";
                    }
                    else if (Convert.ToInt32(e.Value) == 3)
                    {
                        e.Value = "长租带驾";
                    }
                    else if (Convert.ToInt32(e.Value) == 4)
                    {
                        e.Value = "短租带驾";
                    }
                }
            }

            if (this.dgvPoints.Columns[e.ColumnIndex].Name == "下单日")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm");
                }
            }
        }

        private int pagerPoints_EventPaging(EventPagingArg e)
        {
            return dgvPointsBind();
        }

        private int dgvPointsBind()
        {
            //string strSearch = strWhere;
            PageData pageData = new PageData(this, pagerPoint, this.overlay);

            pageData.TableName = "PointsDetails left join Reserve on Reserve.Id=PointsDetails.OrderID";
            pageData.PrimaryKey = "PointsDetails.PointsDetailsID";
            pageData.OrderStr = "PointsDetails.InputDate desc";
            pageData.PageIndex = this.pagerPoint.PageCurrent;
            pageData.PageSize = this.pagerPoint.PageSize;
            pageData.QueryCondition = strSearch.ToString();
            pageData.QueryFieldName = "Points,PointRate,RemainPoints,InputDate,InpuTime,Type,PayType,RentReceivable+OvertimeAmount+SuperDriverAmount as totals";

            dgvPoints.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvPoints.DataSource = this.pagerPoint.bindingSource;
            return pageData.TotalCount;
        }

        string strSearch = "";
        private void tabList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.tabControl1.SelectedIndex)
            {
                case 5:
                    if (CustId == 0)
                        strSearch = "PointsDetails.UserID='-1'";
                    else
                        strSearch = "PointsDetails.UserID='" + model.Id + "'";
                    this.pagerPoint.PageCurrent = 1;
                    this.pagerPoint.Bind();
                    break;
            }
        }

        private void dgvPoints_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvPoints.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvPoints.Rows.Count; )
                {
                    this.dgvPoints.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }
    }
}
